<?php


use Phinx\Seed\AbstractSeed;
use Faker\Factory as FakerFactory;
use App\Library\Utils\Arr;

class QuestionTagSeeder extends AbstractSeed
{

    public function getDependencies(): array
    {
        return [
            'QuestionSeeder',
            'TagSeeder',
        ];
    }

    /**
     * Run Method.
     *
     * Write your database seeder using this method.
     *
     * More information on writing seeders is available here:
     * https://book.cakephp.org/phinx/0/en/seeding.html
     */
    public function run()
    {
        $faker = FakerFactory::create('zh_CN');

        $tags = $this->fetchAll('SELECT * FROM kg_tag');
        $questions = $this->fetchAll('SELECT * FROM kg_question');

        $rows = [];
        for ($i = 0; $i < 10; $i++) {
            $tag = Arr::rand($tags);
            $question = Arr::rand($questions);

            $rows[] = [
                'question_id' => $question['id'],
                'tag_id' => $tag['id'],
                'create_time' => $faker->unixTime,
            ];
        }
        $this->table('kg_question_tag')->insert($rows)->saveData();
    }
}
